import Vue from "vue";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
import router from './router'

import { postRequest } from "@/utils/api";
import { putRequest } from "@/utils/api";
import { deleteRequest } from "@/utils/api";
import { getRequest } from "@/utils/api";
import store from "./store";
import {initMenu} from '@/utils/menus'
import 'font-awesome/css/font-awesome.min.css'
Vue.use(ElementUI);

Vue.config.productionTip = false;

Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.getRequest = getRequest;

// 配置路由守卫
router.beforeEach((to, from, next) => {
  if (window.sessionStorage.getItem("token")) {
    initMenu(router, store);
    if (!window.sessionStorage.getItem("user")) {
      return getRequest('/admin/info').then(resp => {
        if (resp) {
          //存入用户信息
          window.sessionStorage.setItem("user", JSON.stringify(resp));
          store.commit('INIT_CURRENTAdmin',resp);
          next(to.fullPath)
        }
      });
    }
    next();
  } else {
    if (to.path == '/') {
      next();
    } else {
      next('/?redirect=' + to.path);
    }
  }
})

new Vue({
  render: h => h(App),
  router,
  store
}).$mount('#app')
